package com.vcs.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vcs.dao.MailDAO;
import com.vcs.dao.MailDAOImpl;
import com.vcs.domain.Email;
import com.vcs.util.MailUtilRunnable;

import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class MailAction extends ActionSupport implements SessionAware,
		RequestAware, ModelDriven<Email> {
	Log log = LogFactory.getLog(MailAction.class);

	private Email email = new Email();
	MailDAO mailDao = new MailDAOImpl();
	private Map<String, Object> sessionMap;
	private Map<String, Object> requestMap;

	/**
	 * Handles Bulk email sending by admin.
	 */
	public String getEmailAddresses() {
		log.info("getEmailAddresses()...");
		List<String> emailIdList = mailDao.getMailIdList();
		log.info(emailIdList);

		// create a string of email ids from the emailIdList
		String emailIds = emailIdList.toString();
		emailIds = emailIds.replace("[", " ");
		emailIds = emailIds.replace("]", " ");
		log.info(emailIds);

		Map<String, Object> context = new HashMap<String, Object>();
		context.put("toId", getText("email.to"));
		context.put("fromId", getText("email.from"));
		
		// prepare email object
		email.setTo(getText("email.to"));
		email.setFrom(getText("email.from"));
		email.setBcc(emailIds);
		// this is for TO section of email
		//requestMap.put("toId", getText("email.to"));
		//requestMap.put("fromId", getText("email.from"));

		// this will be set into the BCC of email form
		//requestMap.put("emailIds", emailIds);
		return SUCCESS;
	}

	/**
	 * Sends the bulk email posted by admin
	 * 
	 * @return success
	 */
	public String sendBulkMail() {
		log.info("sendBulkMail()...");
		// Validate email form
		validateEmailForm();
		if (hasFieldErrors()) {
			addActionError("There are some errors that need to be resolved before sending the mail.");
			return INPUT;
		}

		log.info("***********************" + email);
		// TODO create a thread to send an email using mail util runnable
		// 1. Create MailUtilRunnable object
		MailUtilRunnable mRunnable = new MailUtilRunnable(email.getFrom(),
				email.getTo(), email.getBcc(), email.getSubject(),
				email.getBody());
		// 2. Create a thread and pass an instance of mailUtilRunnable
		Thread t = new Thread(mRunnable);
		t.start();
		log.info("Sending of bulk email initiated in a new Thread");
		return SUCCESS;
	}

	/**
	 * Validate email form
	 * 
	 * @return
	 */
	private void validateEmailForm() {
		// TO
		if (email.getTo() == null || email.getTo().isEmpty()) {
			addFieldError("to",
					"There should be atleast one email address in \"To\" field");
		}

		// FROM
		if (email.getFrom() == null || email.getFrom().isEmpty()) {
			addFieldError("from",
					"There should be atleast one email address in \"From\" field");
		}
		// CC can be blank as well
		

		// BCC
		if (email.getBcc() == null || email.getBcc().isEmpty()) {
			addFieldError("bcc",
					"There should be atleast one email address in \"Bcc\" field");
		}

		// SUBJECT
		if (email.getSubject() == null || email.getSubject().isEmpty()) {
			addFieldError("subject", "Email Subject can not be empty");
		}
		// BODY
		if (email.getBody() == null || email.getBody().isEmpty()) {
			addFieldError("body", "Email Body can not be empty");
		}
	}

	public Email getEmail() {
		return email;
	}

	public void setEmail(Email email) {
		this.email = email;
	}

	@Override
	public Email getModel() {
		return email;
	}

	@Override
	public void setRequest(Map<String, Object> requestMap) {
		this.requestMap = requestMap;

	}

	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;

	}

}
